package com.tl.sun.ui.activity;

import android.os.Bundle;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.tl.sun.AppContext;
import com.tl.sun.OkHeader;
import com.tl.sun.R;
import com.tl.sun.model.BaseModel;
import com.tl.sun.model.MealEnetity;
import com.tl.sun.utils.ToastUtil;
import com.tl.sun.utils.common.TimeUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by apple on 16/10/13.
 */

public class MealActivity extends BaseActivity {


    @Bind(R.id.rl_generic_back)
    RelativeLayout mRlGenericBack;
    @Bind(R.id.tv_generic_title)
    TextView mTvGenericTitle;
    @Bind(R.id.tv_meal_time)
    TextView mTvMealTime;
    @Bind(R.id.tv_meal_start)
    TextView mTvMealStart;
    @Bind(R.id.tv_meal_end)
    TextView mTvMealEnd;
    @Bind(R.id.tv_meal_residue)
    TextView mTvMealResidue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_meal);
        ButterKnife.bind(this);
        init();
    }

    @Override
    protected void onStart() {
        super.onStart();
        getMeal();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelTag(this);
    }

    public void init() {
        mTvGenericTitle.setText("套餐余量");
    }

    @OnClick(R.id.rl_generic_back)
    public void onClick() {
        onBackPressed();
    }


    private void getMeal() {


        OkGo.getInstance().addCommonHeaders(OkHeader.getCommonHeadToken(this)).post(AppContext.getInstance().getBaseUrl()+"account/get_info")
                .tag(this)
                .upJson(mapSession())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Gson gson = new Gson();

                        BaseModel<MealEnetity> baseModel = gson.fromJson(s, new TypeToken<BaseModel<MealEnetity>>() {
                        }.getType());
                        if (baseModel.getRet().equals("0")) {
                            mTvMealEnd.setText(TimeUtils.milliseconds2String(baseModel.getData().getExpire_time(), TimeUtils.DEFAULT_SEF));
                            mTvMealResidue.setText(baseModel.getData().getLast_time_format());
                        } else {
                            ToastUtil.showToast(baseModel.getMsg());
                        }
                    }
                });
    }

}
